package com.superb.snake;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

/**
 * @author Superb
 * @date 2020/9/15 - 18:49
 * @E_mail superb12580@163.com
 */
public class GamePanel extends JPanel implements KeyListener, ActionListener {

    private int length;

    private int[] snakeX = new int[600];
    private int[] snakeY = new int[600];
    //运动方向 默认向右
    private String fx = "R";

    private int foodX;
    private int foodY;

    private Random random = new Random();


    //是否开始
    private boolean isStart = false;

    //是否失败
    private boolean isFail = false;

    private Timer timer = new Timer(120,this);

    public GamePanel(){
        init();
        //获取监听
        this.setFocusable(true);
        this.addKeyListener(this);

        timer.start();
    }

    public void init(){
        length = 3;
        snakeX[0] = 100;snakeY[0] = 255;
        snakeX[1] = 75;snakeY[1] = 255;
        snakeX[2] = 50;snakeY[2] = 255;


        foodX = 25 + 25 * random.nextInt(34);
        foodY = 155 + 25 * random.nextInt(24);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);//清屏
        this.setBackground(Color.WHITE);

        //绘制头部广告栏
        GameData.header.paintIcon(this,g,190,10);

        //绘制游戏区域
        g.fillRect(25,155,850,600);

        //画一条静态蛇
        GameData.aImg.paintIcon(this,g,snakeX[0],snakeY[0]);

        for (int i = 1; i<length; i++){
            GameData.bImg.paintIcon(this,g,snakeX[i],snakeY[i]);
        }

        //画积分
        g.setColor(Color.BLACK);
        g.setFont(new Font("微软雅黑",Font.BOLD,18));
        g.drawString("长度："+length,750,35);
        g.drawString("积分："+(length-3)*5,750,55);

        //食物
        GameData.bImg.paintIcon(this,g,foodX,foodY);

        //
        if (!isStart){
            g.setColor(Color.WHITE);
            g.setFont(new Font("微软雅黑",Font.BOLD,40));
            g.drawString("按下空格开始游戏",300,300);
        }
        if (isFail){
            g.setColor(Color.WHITE);
            g.setFont(new Font("微软雅黑",Font.BOLD,40));
            g.drawString("游戏失败，按下空格重新开始",300,300);
        }
    }



    @Override
    public void keyPressed(KeyEvent e) {
        //按下
        //接收键盘输入
        int keyCode = e.getKeyCode();
        if (keyCode == KeyEvent.VK_SPACE){
            if (isFail){
                isFail = false;
                init();
            }else {
                isStart = !isStart;
            }
            //刷新页面
            repaint();
        }

        if ((keyCode == KeyEvent.VK_LEFT || keyCode == KeyEvent.VK_A) && !fx.equals("L") && !fx.equals("R")){
            fx = "L";
        }
        if ((keyCode == KeyEvent.VK_RIGHT || keyCode == KeyEvent.VK_D) && !fx.equals("R") && !fx.equals("L")){
            fx = "R";
        }
        if ((keyCode == KeyEvent.VK_W || keyCode == KeyEvent.VK_UP) && !fx.equals("U") && !fx.equals("D")){
            fx = "U";
        }
        if ((keyCode == KeyEvent.VK_DOWN || keyCode == KeyEvent.VK_S) && !fx.equals("D") && !fx.equals("U")) {
            fx = "D";
        }
    }



    //执行定时操作
    @Override
    public void actionPerformed(ActionEvent e) {
        if (isStart && !isFail){
            for (int i = length-1; i>0; i--){
                snakeX[i] = snakeX[i-1];
                snakeY[i] = snakeY[i-1];
            }

            //头部移动
            if (fx.equals("R")){
                //右
                snakeX[0] += 25;
                //边界
                if (snakeX[0]>850){
                    snakeX[0] = 25;
                }
            }else if (fx.equals("U")){
                //上
                snakeY[0] -=25;
                //边界
                if (snakeY[0]<155){
                    snakeY[0] = 755;
                }
            }else if (fx.equals("L")){
                //左
                snakeX[0] -= 25;
                //边界
                if (snakeX[0]<25){
                    snakeX[0] = 850;
                }
            }else {
                //下
                snakeY[0] += 25;
                //边界
                if (snakeY[0]>755){
                    snakeY[0] = 155;
                }
            }

            //判输
            for (int i = 1; i<length; i++){
                if (snakeX[0] == snakeX[i] && snakeY[0] == snakeY[i]){
                    isFail = true;
                }
            }

            //吃
            if (snakeX[0] == foodX && snakeY[0] == foodY){
                length++;
                foodX = 25 + 25 * random.nextInt(34);
                foodY = 155 + 25 * random.nextInt(24);
            }


            //刷新界面
            repaint();
        }
        timer.start();
    }




    @Override
    public void keyTyped(KeyEvent e) {
        //按下弹起
    }

    @Override
    public void keyReleased(KeyEvent e) {
        //释放时
    }


}
